package com.juma.cartridge.toker;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.util.Pool;

public class ApplyUtils {

    public static boolean apply(Pool<Jedis> jedisPool,Token token) {
        
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            SetParams param = SetParams.setParams().nx().ex(token.getTimeoutInSec());
            String replyCode = jedis.set(token.getKey(), "", param);
            if(replyCode.equals("OK")) {
                return true;
            }
            throw new IllegalStateException("生成token生成:Redis repyCode:"+replyCode);
        }finally {
            if(jedis!=null) {
                jedis.close();
            }
        }
        
    }
    
}
